package com.vcg.filter.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
import com.vcg.filter.beans.BaseEntity;
import com.vcg.filter.beans.PhotoEntity;
import com.vcg.filter.service.AbstractService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author fyq on
 * @created by 2016/6/6
 */
public class PhotoServiceImpl extends AbstractService {

    public List<BaseEntity> getList() {
        credentialsProvider = new ClasspathPropertiesFileCredentialsProvider();

        dynamoDBClient = new AmazonDynamoDBClient(credentialsProvider, new ClientConfiguration());
        dynamoDBClient.setEndpoint(dynamodbEndpoint);

        Map<String, AttributeValue> expressionAttributeValues = new HashMap<String, AttributeValue>();
        expressionAttributeValues.put(":vid", new AttributeValue().withS("photo"));
        expressionAttributeValues.put(":vuser", new AttributeValue().withS("13646051"));


        ScanRequest scanRequest = new ScanRequest()
                .withTableName("vcg-activity-slave-test")
                .withFilterExpression("item_type = :vid and user_id = :vuser").withExpressionAttributeValues(expressionAttributeValues)
                .withLimit(50);

        ScanResult result = dynamoDBClient.scan(scanRequest);

        for (Map<String, AttributeValue> item : result.getItems()) {
            JSONObject srcObj = new JSONObject();
            for (String key : item.keySet()) {
                AttributeValue attributeValue = item.get(key);
                String value = attributeValue.getN();
                if (value == null) {
                    value = attributeValue.getS();
                }
                srcObj.put(key, value);
            }
            System.out.println(srcObj.toJSONString());
            String itemType = srcObj.getString("item_type");
            System.out.println(itemType);
        }

        return null;
    }

    public PhotoEntity get(JSONObject jsonObject) {
        jsonObject = getJSON(jsonObject);
        return JSONObject.toJavaObject(jsonObject, PhotoEntity.class);
    }

    public void save(BaseEntity t) {
        dynamoDBMapper.save((PhotoEntity)t);
    }
}
